package com.infindo.frame.data;

import org.apache.http.client.HttpClient;

import com.infindo.frame.data.net.CallbackListener;

 
 

 
public abstract class DataHandler implements Runnable {

	//define DataType
    public static final int DataTypeImage = 1;
    public static final int DataTypeJson  = 2;
    public static final int DataTypeByte  = 3;
    public static final int DataTypeInSt   = 4;
    
    
    public static final int MaxLinkTime = 4;

	private CallbackListener listener;

	private int  DataHandlerNo;
	
	private int  DataHandlerType;
	
	protected HttpClient hc; 
	
	private long workStartTime;
	private long survTime;
	
	private boolean bolWorked;
	private boolean bolStopWork;

	private boolean bolFirstTime;
	private boolean bolNeedTimer;
	
	private int linkTime = 0;

	public int getLinkTime() {
		return linkTime;
	}

	public void setLinkTime(int linkTime) {
		this.linkTime = linkTime;
	}
	
	public void addLinkTime()
	{
		this.linkTime++;
	}

	public DataHandler(int dataHandlerNo, CallbackListener listener) {
		DataHandlerNo = dataHandlerNo;
		this.listener = listener;
	}
	
	public boolean isBolNeedTimer() {
		return bolNeedTimer;
	}

	public void setBolNeedTimer(boolean bolNeedTimer) {
		this.bolNeedTimer = bolNeedTimer;
	}
	
	public boolean isBolFirstTime() {
		return bolFirstTime;
	}

	public void setBolFirstTime(boolean bolFirstTime) {
		this.bolFirstTime = bolFirstTime;
	}
	
	public boolean isBolWorked() {
		return bolWorked;
	}

	public void setBolWorked(boolean bolWorked) {
		this.bolWorked = bolWorked;
	}
	
    public boolean isBolStopWork() {
		return bolStopWork;
	}

	public void setBolStopWork(boolean bolStopWork) {
		this.bolStopWork = bolStopWork;
	}
	
    

	public HttpClient getHc() {
		return hc;
	}

	public void setHc(HttpClient hc) {
		this.hc = hc;
	}

	public long getWorkStartTime() {
		return workStartTime;
	}

	public void setWorkStartTime(long workStartTime) {
		this.workStartTime = workStartTime;
	}

	public long getSurvTime() {
		return survTime;
	}

	public void setSurvTime(long survTime) {
		this.survTime = survTime;
	}
	
	public void setHandlerNo(int type)
	{
		DataHandlerType=type;
	}
	
	public void setHandlerType(int type)
	{
		DataHandlerType=type;
	}
	
	
	public CallbackListener getCallbackListener()
	{
		return listener;
	}
	
	public void setDataHandlerNo(int handlerNo)
	{
		this.DataHandlerNo = handlerNo;
	}
	
	public int getDataHandlerNo()
	{
		return DataHandlerNo;
	}
	
	public int getHandlerType()
	{
		return DataHandlerType;
	}
	
	public boolean isHandlerImage()
	{
		return DataHandlerType==DataTypeImage;
	}
	
	public synchronized void stopConnect()
	{
		setBolStopWork(true);
		try {
			if(hc!=null)
			{
				hc.getConnectionManager().closeExpiredConnections();
				hc.getConnectionManager().shutdown();
				hc = null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public synchronized void startConnect()
	{
		setBolStopWork(false);
		setWorkStartTime(System.currentTimeMillis());
		setSurvTime(DataProcessor.survTime);
	}
	
}
